/**
 * Created by Administrator on 2016/7/14.
 */
$(document).ready(function() {
    //发送取消跳转
    var str = window.location.search;
    var order_num = str.replace(/[^0-9]+/g, '');
    $("header p:first-child").click(function(){
        history.go(-1);
    });
    var user_token =  sessionStorage.user_token;
    var u_id = sessionStorage.u_id;

    //点击图片触发上传事件
    $(document).ready(function(){
        $(".imgGroup .defaultImg").click(function(){
            $("#uploadifive-file_upload input:last-child").click();
        })
    });
    //上传图片
    $(function() {
        var splik;
        $('#file_upload').uploadifive({
            'fileObjName': 'upload',
            'auto'             : true,
            'formData'         : {
                'user_token'     : user_token,
                'device':'wap',
                'category':"note_image",
                'u_id':u_id
            },
            'removeCompleted' : true,
            'multi' : true,
            'uploadLimit' : 4,
            'simUploadLimit':10,
            'fileType' : 'image/*',
            //'fileSizeLimit'   : 5242880,
            'queueID'          : 'queue',
            'uploadScript'     : commonval+'/uploadLiveImage',
            'onUploadComplete' : function(file, data) {
                splik = JSON.parse(data);
                //图片回调展示
                $(".upLoad img").attr("src",commonval+splik.data);
                $(".joinProcess2 #queue a").css("top","-25px");
                var img = $("<section><img class=\"userImg\"></section>");
                var src = commonval+splik.data;
                img.children(".userImg").attr("src",src);
                img.insertBefore($(".defaultImg").parent("section"));
                //imgGroup展示区域
                var length = $(".imgGroup .userImg").length;
                if(length>3){
                    $(".defaultImg").css("display","none");
                }
            }
        });
    });
    //写日记
    $("header p:last-child").click(function(){
        var note_image;
        var image_num = $(".imgGroup section").length-1;
        note_image =$(".imgGroup section").eq(0).children(".userImg").attr("src");
        if(note_image!=undefined){
            var describe_img = note_image.substr(length);
        }
        for(var i=1;i<image_num;i++){
            var src = $(".imgGroup .userImg").eq(i).attr("src");
            src = src.substr(length);
            describe_img = describe_img+";"+src;
        }
        var u_id = sessionStorage.u_id;
        var note_content = $(".mainPart textarea").val();
            var str = window.location.search;
        var order_num = str.replace(/[^0-9]+/g, '');
        var image_num = $(".imgGroup section").length-1;
        $.post(commonval+"/order_note", {
            user_token: user_token,
            device:'wap',
            order_num: order_num,
            u_id:u_id,
            note_content:note_content,
            image_num:image_num,
            note_image:describe_img
        }, function (data) {
            if(data.status == 1){
                createWindow("生发日记书写成功");
                location.href="hairDairy.html?order_no="+order_num;
                sessionStorage.book_id = data.data.book_id;
            }else{
                createWindow(data.msg);
            }
        });
    });
});
//
function createWindow(msg) {
    var window =$("<section class='showMsg'></section>");
    var show = "<p>"+msg+"</p>";
    window.append(show);
    $("body").append(window);
    setTimeout("$('.showMsg').remove()",3000);
}